﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
///fiterCondition 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。 
// [System.Web.Script.Services.ScriptService]
public class fiterCondition : System.Web.Services.WebService
{

    public fiterCondition()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    /// <summary>
    /// 配置sld文件的台站部分
    /// </summary>
    /// <param name="zoom">放大级别</param>
    /// <param name="stations">显示状态变化代号数组</param>
    private void GenerateSDLFileByStation(int zoom, string[] stations)
    {
        //地图样式文件路径
        string path = Server.MapPath("../FiberNetMap/sld/" + GetCookies.NodeID + "/" + GetCookies.UserIsn + "/" + zoom.ToString() + ".xml");

        System.Xml.XmlDocument sldDoc = new System.Xml.XmlDocument();
        sldDoc.Load(path);
        //设置命名空间
        System.Xml.XmlNamespaceManager xnm = new System.Xml.XmlNamespaceManager(sldDoc.NameTable);
        xnm.AddNamespace("sld", "http://www.opengis.net/sld");
        xnm.AddNamespace("ogc", "http://www.opengis.net/ogc");
        //查找FiberRMS:MAP_STATION_GEO图层所有Rule节点
        System.Xml.XmlNodeList Rules = sldDoc.SelectNodes("//sld:NamedLayer[sld:Name='FiberRMS:MAP_STATION_GEO']/sld:UserStyle/sld:FeatureTypeStyle/sld:Rule", xnm);
        foreach (System.Xml.XmlNode Rule in Rules)//遍历所有Rule节点
        {
            foreach (string stationCode in stations)//遍历显示状态发生变化的台站代号
            {
                var filter = Rule.SelectSingleNode("ogc:Filter[ogc:PropertyIsEqualTo/ogc:Literal='" + stationCode + "']", xnm);//查找Rule节点下是否有代号为stationCode的过滤配置
                if (filter != null)//如果有
                {
                    filter.ParentNode.RemoveChild(filter);//删除过滤配置（不显示这个台站）
                }
                else//如果没有
                {
                    //创建过滤配置节
                    System.Xml.XmlElement ogcFilter = sldDoc.CreateElement("ogc", "Filter", "http://www.opengis.net/ogc");
                    System.Xml.XmlElement ogcPropertyIsEqualTo = sldDoc.CreateElement("ogc", "PropertyIsEqualTo", "http://www.opengis.net/ogc");
                    System.Xml.XmlElement ogcPropertyPropertyName = sldDoc.CreateElement("ogc", "PropertyName", "http://www.opengis.net/ogc");
                    ogcPropertyPropertyName.InnerText = "STATIONCODE";
                    System.Xml.XmlElement ogcLiteral = sldDoc.CreateElement("ogc", "Literal", "http://www.opengis.net/ogc");
                    ogcLiteral.InnerText = stationCode;
                    //把过滤配置节追加到样式文档
                    //System.Xml.XmlNode stationTypeNode = Rule.SelectSingleNode("ogc:Filter[PropertyIsEqualTo/PropertyName='X_STATIONTYPE']",xnm);
                    //if (stationTypeNode != null)//如果存在“台站类型”配置节点
                    //{
                    //    Rule.InsertAfter(ogcFilter, stationTypeNode);//把“台站代号过滤”节点放在“台站类型”配置节点之后
                    //}
                    //else
                    //{
                    //    Rule.InsertAfter(ogcFilter, Rule.FirstChild);//否则，把“台站代号过滤”节点放在Rule的第一个子节点之后
                    //}
                    Rule.AppendChild(ogcFilter);
                    //添加“台站代号配置”节点子节点
                    ogcFilter.AppendChild(ogcPropertyIsEqualTo);
                    ogcPropertyIsEqualTo.AppendChild(ogcPropertyPropertyName);
                    ogcPropertyIsEqualTo.AppendChild(ogcLiteral);
                }
            }
        }
        sldDoc.Save(path);//保存样式文件

    }
    /// <summary>
    /// 配置sld文件的线路部分
    /// </summary>
    /// <param name="zoom">放大级别</param>
    /// <param name="stations">显示状态变化代号数组</param>
    public  void GenerateSDLFileByLine(int zoom, string[] lineIDs)
    {
        //地图样式文件路径
        string path = Server.MapPath("../Topo/sld/" + GetCookies.NodeID + "/" + GetCookies.UserIsn + "/" + zoom.ToString() + ".xml");

        System.Xml.XmlDocument sldDoc = new System.Xml.XmlDocument();
        sldDoc.Load(path);
        //设置命名空间
        System.Xml.XmlNamespaceManager xnm = new System.Xml.XmlNamespaceManager(sldDoc.NameTable);
        xnm.AddNamespace("sld", "http://www.opengis.net/sld");
        xnm.AddNamespace("ogc", "http://www.opengis.net/ogc");
        //查找FiberRMS:MAP_STATIONLINK_GEO图层所有Rule节点
        System.Xml.XmlNodeList Rules = sldDoc.SelectNodes("//sld:NamedLayer[sld:Name='FiberRMS:MAP_STATIONLINK_GEO']/sld:UserStyle/sld:FeatureTypeStyle/sld:Rule", xnm);
        foreach (System.Xml.XmlNode Rule in Rules)//遍历所有Rule节点
        {
            foreach (string lineID in lineIDs)//遍历显示状态发生变化的线路ID
            {
                var filter = Rule.SelectSingleNode("ogc:Filter[ogc:PropertyIsEqualTo/ogc:Literal='" + lineID + "']", xnm);//查找Rule节点下是否有代号为lineID的过滤配置
                if (filter != null)//如果有
                {
                    filter.ParentNode.RemoveChild(filter);//删除过滤配置（不显示这个线路）
                }
                else//如果没有
                {
                    //创建过滤配置节
                    System.Xml.XmlElement ogcFilter = sldDoc.CreateElement("ogc", "Filter", "http://www.opengis.net/ogc");
                    System.Xml.XmlElement ogcPropertyIsEqualTo = sldDoc.CreateElement("ogc", "PropertyIsEqualTo", "http://www.opengis.net/ogc");
                    System.Xml.XmlElement ogcPropertyPropertyName = sldDoc.CreateElement("ogc", "PropertyName", "http://www.opengis.net/ogc");
                    ogcPropertyPropertyName.InnerText = "LINEID";
                    System.Xml.XmlElement ogcLiteral = sldDoc.CreateElement("ogc", "Literal", "http://www.opengis.net/ogc");
                    ogcLiteral.InnerText = lineID;
                    //把过滤配置节添加到样式文档
                    Rule.InsertBefore(ogcFilter, Rule.FirstChild);
                    //添加“台站代号配置”节点子节点
                    ogcFilter.AppendChild(ogcPropertyIsEqualTo);
                    ogcPropertyIsEqualTo.AppendChild(ogcPropertyPropertyName);
                    ogcPropertyIsEqualTo.AppendChild(ogcLiteral);
                }
            }
        }
        sldDoc.Save(path);//保存样式文件

    }
    /// <summary>
    /// 检查当前用户网络组织图sld文件及其路径是否存在，如果不存在则创建
    /// </summary>
    /// <param name="userISN"></param>
    /// <param name="userNodeID"></param>
    public void CheckSLDFile(string userISN, string userNodeID)
    {
        string sldNodePath = Server.MapPath("sld/" + userNodeID);
        if (!System.IO.Directory.Exists(sldNodePath))
        {
            System.IO.Directory.CreateDirectory(sldNodePath);
            //向sld模板中添加权限配置节，权限规则：NODEID LIKE 'userNodeID%'
            System.Xml.XmlDocument sldDoc = new System.Xml.XmlDocument();
            sldDoc.Load(Server.MapPath("sld/base.xml"));
            System.Xml.XmlNamespaceManager xnm = new System.Xml.XmlNamespaceManager(sldDoc.NameTable);
            xnm.AddNamespace("sld", "http://www.opengis.net/sld");
            xnm.AddNamespace("ogc", "http://www.opengis.net/ogc");
            System.Xml.XmlNodeList Ands = sldDoc.SelectNodes("//sld:NamedLayer[sld:Name='FiberRMS:MAP_STATION_GEO']/sld:UserStyle/sld:FeatureTypeStyle/sld:Rule/ogc:Filter/ogc:And", xnm);
            foreach (System.Xml.XmlNode And in Ands)//遍历所有过滤节点下的And节点，And节点是在sld模板中添加的
            {
                //创建权限过滤配置节
                System.Xml.XmlElement ogcPropertyIsLike = sldDoc.CreateElement("ogc", "PropertyIsLike", "http://www.opengis.net/ogc");
                ogcPropertyIsLike.SetAttribute("wildCard", "%");//通配符
                ogcPropertyIsLike.SetAttribute("singleChar", "_");//单字符匹配符
                ogcPropertyIsLike.SetAttribute("escapeChar", "!");//匹配“匹配字符”本身的符号
                System.Xml.XmlElement ogcPropertyPropertyName = sldDoc.CreateElement("ogc", "PropertyName", "http://www.opengis.net/ogc");
                ogcPropertyPropertyName.InnerText = "NODEID";
                System.Xml.XmlElement ogcLiteral = sldDoc.CreateElement("ogc", "Literal", "http://www.opengis.net/ogc");
                ogcLiteral.InnerText = userNodeID + "%";

                And.AppendChild(ogcPropertyIsLike);
                //添加“权限配置”节点子节点
                ogcPropertyIsLike.AppendChild(ogcPropertyPropertyName);
                ogcPropertyIsLike.AppendChild(ogcLiteral);
            }
            sldDoc.Save(sldNodePath + "/base.xml");

            //System.IO.File.Copy(Server.MapPath("sld/base.xml"), sldNodePath + "/base.xml");
        }

        string sldUserPath = sldNodePath + "/" + userISN;
        if (!System.IO.Directory.Exists(sldUserPath))
        {
            System.IO.Directory.CreateDirectory(sldUserPath);

            int maxZoomLevel = 15;//地图最大放大级别
            for (int i = 0; i <= maxZoomLevel; i++)//初始化15个放大级别的
            {
                System.IO.File.Copy(sldNodePath + "/base.xml", sldUserPath + "/" + i.ToString() + ".xml");
            }
        }

    }
}

